package com.ruoyi.market.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.market.domain.Price;
import com.ruoyi.market.domain.Price;
import com.ruoyi.market.mapper.PriceMapper;
import com.ruoyi.market.service.IPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 价格 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class PriceServiceImpl implements IPriceService
{

    @Autowired
    private PriceMapper priceMapper;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询价格列表
     * 
     * @param price 价格信息
     * @return 价格信息集合信息
     */
    @Override
    public List<Price> selectPriceList(Price price)
    {
        return priceMapper.selectPriceList(price);
    }

    /**
     * 通过价格ID查询价格
     * 
     * @param priceId 价格ID
     * @return 价格对象信息
     */
    @Override
    public Price selectPriceById(Long priceId)
    {
        return priceMapper.selectPriceById(priceId);
    }

    /**
     * 校验是否唯一
     *
     * @param price 价格信息
     * @return 结果
     */
    @Override
    public boolean checkPriceUnique(Price price)
    {
        Long priceId = StringUtils.isNull(price.getPriceId()) ? -1L : price.getPriceId();
        Price info = priceMapper.checkPriceUnique(price.getLevelId(), price.getProjectId(), price.getUnitId(), price.getShopId());
        if (StringUtils.isNotNull(info) && info.getPriceId().longValue() != priceId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增保存价格信息
     * 
     * @param price 价格信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPrice(Price price)
    {
        // 新增价格信息
        int rows = priceMapper.insertPrice(price);
        return rows;
    }

    /**
     * 修改保存价格信息
     * 
     * @param price 价格信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePrice(Price price)
    {
        return priceMapper.updatePrice(price);
    }

    /**
     * 修改价格状态
     * 
     * @param price 价格信息
     * @return 结果
     */
    @Override
    public int updatePriceStatus(Price price)
    {
        return priceMapper.updatePrice(price);
    }

    /**
     * 随机单是否可用
     *
     * @param price 价格信息
     * @return 结果
     */
    @Override
    public int updatePriceIsAble(Price price)
    {
        return priceMapper.updatePrice(price);
    }

    /**
     * 批量删除价格信息
     * 
     * @param priceIds 需要删除的价格ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePriceByIds(Long[] priceIds) { return priceMapper.deletePriceByIds(priceIds); }

    /**
     * 导入价格
     *
     * @param priceList 价格列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importPrice(List<Price> priceList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(priceList) || priceList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Price price : priceList)
        {
            try
            {
                // 是唯一的
                if (checkPriceUnique(price))
                {
                    BeanValidators.validateWithException(validator, price);
                    price.setCreateBy(operName);
                    priceMapper.insertPrice(price);
                    successNum++;
                    successMsg.append("<br/>序号" + successNum + "、导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, price);
                    price.setUpdateBy(operName);
                    priceMapper.updatePrice(price);
                    successNum++;
                    successMsg.append("<br/>序号" + successNum + "、更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>序号" + failureNum + "、已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>序号" + failureNum + "、导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
